package org.piaohao.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.piaohao.model.Rule;
import org.piaohao.util.string.JsonUtil;
import org.piaohao.util.string.UUIDGenerator;

/**
 * Created on : 2013-5-10, 10:23:10
 *
 * @author piaohao
 */
public class RuleController extends Controller {

    public static final String moduleName = "规章制度模块";

    public void index() {
        render("/pages/rule/rule.jsp");
    }

    public void list() {
        try {
            Integer start = getParaToInt("start");
            Integer limit = getParaToInt("limit");
            String orgId = (String) getSessionAttr("orgId");
            List<Record> rules = null;
            Long total = 0L;
            if (orgId.equals("0")) {
                rules = Db.find("select r.*, o.name orgName from t_rule r left join t_organization o on r.org_id=o.id limit ?,?", start, limit);
                total = Db.queryLong("select count(*) from t_rule");
            } else {
                rules = Db.find("select r.* o.name orgName from t_rule r left join t_organization o on r.org_id=o.id where r.org_id=? limit ?,?", orgId, start, limit);
                total = Db.queryLong("select count(*) from t_rule where org_id=?", orgId);
            }
            renderJson(JsonUtil.printGrid(total, rules));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getRuleById() {
        try {
            Rule rule = Rule.dao.findById(getPara("ruleId"));
            Map map = new HashMap();
            map.put("rule", rule);
            renderJson(JsonUtil.printSuccess(true, "成功", map));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void add() {
        try {
            Rule rule = getModel(Rule.class);
            String para1 = getPara("publish_date");
            DateFormat df = new SimpleDateFormat("yy-MM-dd");
            Date pubDate = df.parse(para1);
            rule.set("publish_date", new Timestamp(pubDate.getTime()));
            String para2 = getPara("reg_date");
            Date regDate = df.parse(para2);
            rule.set("reg_date", new Timestamp(regDate.getTime()));
            if (rule.getStr("id") == null || rule.getStr("id").trim().equals("")) {
                rule.set("id", UUIDGenerator.getUUID());
                rule.save();
            }
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void update() {
        try {
            Rule rule = getModel(Rule.class);
            String para1 = getPara("publish_date");
            DateFormat df = new SimpleDateFormat("yy-MM-dd");
            Date pubDate = df.parse(para1);
            rule.set("publish_date", new Timestamp(pubDate.getTime()));
            String para2 = getPara("reg_date");
            Date regDate = df.parse(para2);
            rule.set("reg_date", new Timestamp(regDate.getTime()));
            rule.update();
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void delete() {
        try {
            String[] idArr = getParaValues("ids");
            for (int i = 0; i < idArr.length; i++) {
                Rule.dao.deleteById(idArr[i]);
            }
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void showDetail() {
        try {
            String id = getPara(0);
            Rule rule = Rule.dao.findById(id);
            setAttr("rule", rule);
            render("/pages/rule/detail.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }
}
